package com.mediacloud.app.newsmodule.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.mediacloud.app.assembly.util.FunKt;
import com.mediacloud.app.assembly.util.ImageKitKt;
import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;

/**
 * 这改成默认占位图用 app后台设置的那张
 *
 * @author x
 */
public class PictureViewX extends PictureView {


    public PictureViewX(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
//        initDefaultImg(context);
    }

    /**
     * 设置占位图
     *
     * @param context
     */
    protected Drawable initDefaultImg(final Context context) {
        float percent = 16.0f / 9.0f;
        int width = context.getResources().getDisplayMetrics().widthPixels;
        int height = (int) (width / percent);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
        Drawable drawable = new BitmapDrawable(bitmap);
        Canvas canvas = new Canvas(bitmap);
        int color = AppFactoryGlobalConfig.getDefaultImagLoadBackground();
        canvas.drawColor(color, Mode.ADD);
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setFilterBitmap(true);
        paint.setStyle(Style.FILL);
        canvas.drawRect(new Rect(0, 0, width, height), paint);
        Bitmap defaultLoading = ((BitmapDrawable) AppFactoryGlobalConfig.getDefaultImageLoadDrawable()).getBitmap();
        Matrix matrix = new Matrix();
        float translateX = 0.0f, translateY = 0.0f;
        if (defaultLoading.getHeight() > height || defaultLoading.getWidth() > width) {
            float scaleX = 1.0f, scaleY = 1.0f;
            float scaleW, scaleH;
            if (defaultLoading.getHeight() > height && defaultLoading.getWidth() > width) {
                scaleY = 1.0f * height / defaultLoading.getHeight();
                scaleX = scaleY;
            } else if (defaultLoading.getHeight() > height) {
                scaleY = 1.0f * height / defaultLoading.getHeight();
                scaleX = scaleY;
            } else if (defaultLoading.getWidth() > width) {
                scaleX = 1.0f * width / defaultLoading.getWidth();
                scaleY = scaleX;
            }
            scaleW = defaultLoading.getWidth() * scaleX;
            scaleH = defaultLoading.getHeight() * scaleY;
            translateX = (width - scaleW) / 2.0f;
            translateY = (height - scaleH) / 2.0f;
            matrix.postScale(scaleX, scaleY);
        } else {
            translateX = (width - defaultLoading.getWidth()) / 2.0f;
            translateY = (height - defaultLoading.getHeight()) / 2.0f;
        }
        matrix.postTranslate(translateX, translateY);
        canvas.drawBitmap(defaultLoading, matrix, paint);
        drawable.draw(canvas);
//		defaultDrawable=drawable;
        setImageDrawable2(drawable);
        return drawable;
    }

    public PictureViewX(Context context, AttributeSet attr) {
        super(context, attr);
//        initDefaultImg(context);
    }

    public void setThumbnailImageURL(String urlStr) {
        setImageResource(R.drawable.cctv7_loading_img);
        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.cctv7_loading_img);
        FunKt.otherLoad(getContext(), ImageKitKt.buildUrl(3, urlStr), new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                setFailedLoad(urlStr, drawable);
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                try {
                    float minScale = 3f;
                    boolean haveToScale = false;
                    int tempViewHeight = getHeight();
                    if (tempViewHeight <= 0) {
                        tempViewHeight = Utility.getScreenHeight(getContext());
                    }
                    if ((resource.getIntrinsicWidth() > 0 && resource.getIntrinsicHeight() > 0) &&
                            (resource.getIntrinsicHeight() > resource.getIntrinsicWidth() &&
                                    Utility.getScreenWidth(getContext()) * 1.0f / tempViewHeight > resource.getIntrinsicWidth() * 1.0f / resource.getIntrinsicHeight())) {

                        int tempHeight = resource.getIntrinsicHeight() * Utility.getScreenWidth(getContext()) / resource.getIntrinsicWidth();
//                            try {
//                                ViewGroup.LayoutParams layoutParams = PictureViewX.this.getLayoutParams();
//                                layoutParams.height = tempHeight;
//                                PictureViewX.this.setLayoutParams(layoutParams);
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
                        minScale = tempHeight * 1.0f / tempViewHeight;
                        PictureViewX.this.setMaxScale(minScale + 2);
                        PictureViewX.this.setMidScale(minScale + 0.75f);
                        PictureViewX.this.setMinScale(minScale);
                        haveToScale = true;
                    }
                    if (haveToScale) {
                        PictureViewX.this.zoomTo(minScale, 0, 0);
                        if (mAttacher != null) {
                            mAttacher.isLongPhoto = true;
                        }
                    } else {
                        if (mAttacher != null) {
                            mAttacher.isLongPhoto = false;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (resource instanceof GifDrawable) {
                    ((GifDrawable) resource).start();
                }
                PictureViewX.this.setImageDrawable(resource);


                return false;
            }
        });
    }

    private void setFailedLoad(String urlStr, Drawable drawable) {
        FunKt.otherLoad(getContext(), urlStr, new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                PictureViewX.this.setImageDrawable(drawable);
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                try {
                    float minScale = 3f;
                    boolean haveToScale = false;
                    int tempViewHeight = getHeight();
                    if (tempViewHeight <= 0) {
                        tempViewHeight = Utility.getScreenHeight(getContext());
                    }
                    if ((resource.getIntrinsicWidth() > 0 && resource.getIntrinsicHeight() > 0) &&
                            (resource.getIntrinsicHeight() > resource.getIntrinsicWidth() &&
                                    Utility.getScreenWidth(getContext()) * 1.0f / tempViewHeight > resource.getIntrinsicWidth() * 1.0f / resource.getIntrinsicHeight())) {

                        int tempHeight = resource.getIntrinsicHeight() * Utility.getScreenWidth(getContext()) / resource.getIntrinsicWidth();
//                            try {
//                                ViewGroup.LayoutParams layoutParams = PictureViewX.this.getLayoutParams();
//                                layoutParams.height = tempHeight;
//                                PictureViewX.this.setLayoutParams(layoutParams);
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
                        minScale = tempHeight * 1.0f / tempViewHeight;
                        PictureViewX.this.setMaxScale(minScale + 2);
                        PictureViewX.this.setMidScale(minScale + 0.75f);
                        PictureViewX.this.setMinScale(minScale);
                        haveToScale = true;
                    }
                    if (haveToScale) {
                        PictureViewX.this.zoomTo(minScale, 0, 0);
                        if (mAttacher != null) {
                            mAttacher.isLongPhoto = true;
                        }
                    } else {
                        if (mAttacher != null) {
                            mAttacher.isLongPhoto = false;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (resource instanceof GifDrawable) {
                    ((GifDrawable) resource).start();
                }
                PictureViewX.this.setImageDrawable(resource);


                return false;
            }
        });
    }

    @Override
    public void setImageURL(String urlStr) {
        setImageURL(urlStr, true);
    }

    public void setImageURL(String urlStr, boolean isShowLoading) {
        if (isShowLoading) {
            setImageResource(R.drawable.cctv7_loading_img);
        }
        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.cctv7_loading_img);//initDefaultImg(context);
        FunKt.otherLoad(getContext(), urlStr, new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                PictureViewX.this.setImageDrawable(drawable);
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                try {
                    float minScale = 3f;
                    boolean haveToScale = false;
                    int tempViewHeight = getHeight();
                    if (tempViewHeight <= 0) {
                        tempViewHeight = Utility.getScreenHeight(getContext());
                    }
                    int tempViewWidth = getWidth();
                    if (tempViewWidth <= 0) {
                        tempViewWidth = Utility.getScreenWidth(getContext());
                    }

                    if ((resource.getIntrinsicWidth() > 0 && resource.getIntrinsicHeight() > 0) &&
                            (resource.getIntrinsicHeight() > resource.getIntrinsicWidth() &&
                                    tempViewWidth * 1.0f / tempViewHeight > resource.getIntrinsicWidth() * 1.0f / resource.getIntrinsicHeight())) {

                        int tempHeight = resource.getIntrinsicHeight() * tempViewWidth / resource.getIntrinsicWidth();
//                            try {
//                                ViewGroup.LayoutParams layoutParams = PictureViewX.this.getLayoutParams();
//                                layoutParams.height = tempHeight;
//                                PictureViewX.this.setLayoutParams(layoutParams);
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
                        if (resource.getIntrinsicWidth() > tempViewWidth) {
                            float difference = resource.getIntrinsicWidth() * 1.0f / tempViewWidth;
                            minScale = tempHeight * 1.0f / tempViewHeight - difference;
                            PictureViewX.this.setMinScale(minScale);
                            PictureViewX.this.setMidScale(minScale + 0.75f + difference);
                            PictureViewX.this.setMaxScale(minScale + 2 + difference);
                        } else {
                            minScale = tempHeight * 1.0f / tempViewHeight;
                            PictureViewX.this.setMaxScale(minScale + 2);
                            PictureViewX.this.setMidScale(minScale + 0.75f);
                            PictureViewX.this.setMinScale(minScale);
                        }
                        haveToScale = true;
                    }
                    if (haveToScale) {
                        PictureViewX.this.zoomTo(minScale, 0, 0);
                        if (mAttacher != null) {
                            mAttacher.isLongPhoto = true;
                        }
                    } else {
                        if (mAttacher != null) {
                            mAttacher.isLongPhoto = false;
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (resource instanceof GifDrawable) {
                    ((GifDrawable) resource).start();
                }
                PictureViewX.this.setImageDrawable(resource);


                return false;
            }
        });
    }

    public PictureViewX(Context context) {
        super(context);
//        initDefaultImg(context);
    }

}